package com.tencent.sr.iris.activity.dependency;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.tencent.sr.iris.activity.common.config.QuFaConfig;
import com.tencent.sr.iris.activity.common.util.QuFaHttpUtil;
import com.tencent.sr.iris.activity.dependency.dto.common.OperateBoardUtils;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardRankSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.OperateSecondBoardDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.RankAwardConfigDTO;
import com.tencent.sr.iris.activity.interfaces.request.group.AdvisorGroupTaskRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.AdvisorGroupTaskSecondRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.AdvisorGroupTaskThirdRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.GroupTaskRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.GroupTaskSecondRequest;
import com.tencent.sr.iris.activity.interfaces.request.group.RankConfigRequest;
import com.tencent.sr.iris.activity.interfaces.response.group.*;
import com.tencent.sr.iris.activity.dependency.dto.common.GroupTaskBoardUtils;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardCommissionSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardCommissionThirdDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardFirstOrderSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardInviteSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.AdvisorBoardSprintSecondDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.GroupTaskBaseDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.GroupTaskDTO;
import com.tencent.sr.iris.activity.dependency.dto.group.GroupTaskSecondDTO;
import com.tencent.sr.iris.activity.interfaces.response.group.operate.OperateSecondBoardResponse;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import com.tencent.sr.rmall.springbootstarter.utils.JsonUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

;

@Component
@Slf4j
public class GroupTaskBoardDependency {

    @Resource
    private QuFaHttpUtil quFaHttpUtil;
    @Resource
    private QuFaConfig config;

    /**
     * 团长身份-一级任务列表
     *
     * @param request
     * @param url
     * @return
     */
    public PaginationResponse<GroupTaskBoardResponse> groupTaskBoard(PaginationRequest<GroupTaskRequest> request,
            String url) {
        PaginationResponse<GroupTaskBoardResponse> response = new PaginationResponse<>();
        String reqBody = GroupTaskBoardUtils.toGroupTaskBoardBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            System.out.println(data);
            GroupTaskBaseDTO<GroupTaskDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<GroupTaskDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<GroupTaskDTO> data1 = groupTaskBaseDTO.getData();
                List<GroupTaskBoardResponse> list = data1.stream().
                        map(dto -> GroupTaskBoardUtils.toGroupTaskBoardResponse(dto)).collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 团长身份-二级任务列表
     *
     * @param request
     * @param url
     * @return
     */
    public PaginationResponse<GroupTaskBoardSecondResponse> groupTaskSecondBoard(
            PaginationRequest<GroupTaskSecondRequest> request, String url) {
        PaginationResponse<GroupTaskBoardSecondResponse> response = new PaginationResponse<>();
        String reqBody = GroupTaskBoardUtils.toGroupTaskSecondBoardBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<GroupTaskSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<GroupTaskSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<GroupTaskSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<GroupTaskBoardSecondResponse> list = data1.stream()
                        .map(dto -> GroupTaskBoardUtils.toGroupTaskBoardSecondResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 导师身份-一级任务列表
     *
     * @param request
     * @param url
     * @return
     */
    public PaginationResponse<AdvisorBoardResponse> queryAdvisorBoard(
            PaginationRequest<AdvisorGroupTaskRequest> request, String url) {
        PaginationResponse<AdvisorBoardResponse> response = new PaginationResponse<>();
        String reqBody = GroupTaskBoardUtils.toAdvisorBoardBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<AdvisorBoardDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<AdvisorBoardDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<AdvisorBoardDTO> data1 = groupTaskBaseDTO.getData();
                List<AdvisorBoardResponse> list = data1.stream()
                        .map(dto -> GroupTaskBoardUtils.toAdvisorBoardResponse(dto)).collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    public GroupTaskPaginationResponse<OperateSecondBoardResponse> queryAdvisorSecondBoard(
            PaginationRequest<AdvisorGroupTaskSecondRequest> request, String url) {
        String reqBody = GroupTaskBoardUtils.toAdvisorSecondBoardCommissionBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<OperateSecondBoardDTO> advisorSecondDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<OperateSecondBoardDTO>>() {
                    });
            GroupTaskPaginationResponse<OperateSecondBoardResponse> response = JSONObject
                    .parseObject(data.toString(), GroupTaskPaginationResponse.class);
            if (Objects.nonNull(advisorSecondDTO) && CollectionUtils.isNotEmpty(advisorSecondDTO.getData())) {
                List<OperateSecondBoardDTO> data1 = advisorSecondDTO.getData();
                List<OperateSecondBoardResponse> list = data1.stream()
                        .map(dto -> OperateBoardUtils.toOperateSecondBoardResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(advisorSecondDTO.getCurrentPage());
                response.setPageSize(advisorSecondDTO.getPerPage());
                response.setTotalCount(advisorSecondDTO.getTotal());
            }
            return response;
        }
        return new GroupTaskPaginationResponse<>();
    }

    /**
     * 导师身份-团长任务二级页面（邀新有礼）
     *
     * @param request
     * @param url
     * @return
     */
    public PaginationResponse<AdvisorBoardInviteSecondResponse> queryAdvisorSecondBoardInvite(
            PaginationRequest<AdvisorGroupTaskSecondRequest> request, String url) {
        PaginationResponse<AdvisorBoardInviteSecondResponse> response = new PaginationResponse<>();
        String reqBody = GroupTaskBoardUtils.toAdvisorSecondBoardInviteBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<AdvisorBoardInviteSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<AdvisorBoardInviteSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<AdvisorBoardInviteSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<AdvisorBoardInviteSecondResponse> list = data1.stream()
                        .map(dto -> GroupTaskBoardUtils.toAdvisorBoardInviteSecondResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 导师身份-团长任务二级页面（GMV冲刺赛/销量冲刺赛）
     *
     * @param request
     * @param url
     * @return
     */
    public GroupTaskPaginationResponse<AdvisorBoardSprintSecondResponse> queryAdvisorSecondBoardSprint(
            PaginationRequest<AdvisorGroupTaskSecondRequest> request, String url) {
        String reqBody = GroupTaskBoardUtils.toAdvisorSecondBoardSprintBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<AdvisorBoardSprintSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<AdvisorBoardSprintSecondDTO>>() {
                    });
            GroupTaskPaginationResponse<AdvisorBoardSprintSecondResponse> response = JSONObject
                    .parseObject(data.toString(),
                            new TypeReference<GroupTaskPaginationResponse<AdvisorBoardSprintSecondResponse>>() {
                            });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<AdvisorBoardSprintSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<AdvisorBoardSprintSecondResponse> list = data1.stream()
                        .map(dto -> GroupTaskBoardUtils.toAdvisorBoardSprintSecondResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return new GroupTaskPaginationResponse<>();
    }

    /**
     * 导师身份-团长任务二级页面（佣金回滚任务）
     *
     * @param request
     * @param url
     * @return
     */
    public PaginationResponse<AdvisorBoardCommissionSecondResponse> queryAdvisorSecondBoardCommission(
            PaginationRequest<AdvisorGroupTaskSecondRequest> request, String url) {
        PaginationResponse<AdvisorBoardCommissionSecondResponse> response = new PaginationResponse<>();
        response.setDataList(Collections.emptyList());
        response.setTotalCount(0);
        response.setPageSize(request.getPageSize());
        response.setPageNum(request.getPageNum());

        String reqBody = GroupTaskBoardUtils.toAdvisorSecondBoardCommissionBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<AdvisorBoardCommissionSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<AdvisorBoardCommissionSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<AdvisorBoardCommissionSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<AdvisorBoardCommissionSecondResponse> list = data1.stream()
                        .map(dto -> GroupTaskBoardUtils.toAdvisorBoardCommissionSecondResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 导师身份-团长任务二级页面（首单任务）
     *
     * @param request
     * @param url
     * @return
     */
    public PaginationResponse<AdvisorBoardFirstOrderSecondResponse> queryAdvisorSecondBoardFirstOrder(
            PaginationRequest<AdvisorGroupTaskSecondRequest> request, String url) {
        PaginationResponse<AdvisorBoardFirstOrderSecondResponse> response = new PaginationResponse<>();
        response.setDataList(Collections.emptyList());
        response.setTotalCount(0);
        response.setPageSize(request.getPageSize());
        response.setPageNum(request.getPageNum());

        String reqBody = GroupTaskBoardUtils.toAdvisorSecondBoardCommissionBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<AdvisorBoardFirstOrderSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<AdvisorBoardFirstOrderSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<AdvisorBoardFirstOrderSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<AdvisorBoardFirstOrderSecondResponse> list = data1.stream()
                        .map(dto -> GroupTaskBoardUtils.toAdvisorBoardFirstOrderSecondResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 导师身份-团长任务二级页面（排行榜）
     *
     * @param request
     * @param url
     * @return
     */
    public PaginationResponse<AdvisorBoardRankSecondResponse> queryAdvisorSecondRank(
            PaginationRequest<AdvisorGroupTaskSecondRequest> request, String url) {
        PaginationResponse<AdvisorBoardRankSecondResponse> response = new PaginationResponse<>();
        String reqBody = GroupTaskBoardUtils.toAdvisorSecondBoardSprintBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<AdvisorBoardRankSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<AdvisorBoardRankSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<AdvisorBoardRankSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<AdvisorBoardRankSecondResponse> list = data1.stream()
                        .map(dto -> GroupTaskBoardUtils.toAdvisorBoardRankSecondResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 导师身份-团长任务三级页面
     *
     * @param request
     * @return
     */
    public PaginationResponse<GroupTaskBoardSecondResponse> queryAdvisorThirdBoard(
            PaginationRequest<AdvisorGroupTaskThirdRequest> request, String url) {
        PaginationResponse<GroupTaskBoardSecondResponse> response = new PaginationResponse<>();
        response.setDataList(Collections.emptyList());
        response.setTotalCount(0);
        response.setPageSize(request.getPageSize());
        response.setPageNum(request.getPageNum());

        String reqBody = GroupTaskBoardUtils.toAdvisorThirdBoardBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<GroupTaskSecondDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<GroupTaskSecondDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<GroupTaskSecondDTO> data1 = groupTaskBaseDTO.getData();
                List<GroupTaskBoardSecondResponse> list = data1
                        .stream().map(dto -> GroupTaskBoardUtils.toGroupTaskBoardSecondResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 导师身份-团长任务三级页面
     *
     * @param request
     * @return
     */
    public PaginationResponse<AdvisorBoardCommissionThirdResponse> queryAdvisorThirdBoardCommission(
            PaginationRequest<AdvisorGroupTaskThirdRequest> request, String url) {
        PaginationResponse<AdvisorBoardCommissionThirdResponse> response = new PaginationResponse<>();
        response.setDataList(Collections.emptyList());
        response.setTotalCount(0);
        response.setPageSize(request.getPageSize());
        response.setPageNum(request.getPageNum());

        String reqBody = GroupTaskBoardUtils.toAdvisorThirdBoardBody(request);
        String result = quFaHttpUtil.postCommon(config, url, reqBody).toString();
        if (Objects.nonNull(result)) {
            ObjectNode jsonNodes = JsonUtils.parseObject(result);
            JsonNode data = jsonNodes.get("data");
            GroupTaskBaseDTO<AdvisorBoardCommissionThirdDTO> groupTaskBaseDTO = JSONObject.parseObject(data.toString(),
                    new TypeReference<GroupTaskBaseDTO<AdvisorBoardCommissionThirdDTO>>() {
                    });
            if (Objects.nonNull(groupTaskBaseDTO) && CollectionUtils.isNotEmpty(groupTaskBaseDTO.getData())) {
                List<AdvisorBoardCommissionThirdDTO> data1 = groupTaskBaseDTO.getData();
                List<AdvisorBoardCommissionThirdResponse> list = data1
                        .stream().map(dto -> GroupTaskBoardUtils.toAdvisorBoardCommissionThirdResponse(dto))
                        .collect(Collectors.toList());
                response.setDataList(list);
                response.setPageNum(groupTaskBaseDTO.getCurrentPage());
                response.setPageSize(groupTaskBaseDTO.getPerPage());
                response.setTotalCount(groupTaskBaseDTO.getTotal());
            }
            return response;
        }
        return response;
    }

    /**
     * 查询奖励阶段下拉
     *
     * @param request
     * @param url
     * @return
     */
    public List<RankAwardConfigResponse> queryRankConfigs(RankConfigRequest request, String url) {
        List<RankAwardConfigResponse> result = new ArrayList<>();
        List<RankAwardConfigDTO> list = quFaHttpUtil.postList(url, request, RankAwardConfigDTO.class);
        if (CollectionUtils.isNotEmpty(list)) {
            list.stream().forEach(r -> {
                RankAwardConfigResponse response = new RankAwardConfigResponse();
                response.setRankingEnd(r.getRankingEnd());
                response.setRankingStart(r.getRankingStart());
                response.setTarget_id(r.getTargetId());
                response.setRankAwardConfigId(r.getRankAwardConfigId());
                result.add(response);
            });
        }
        return result;
    }

}
